<?php
namespace Common\Model\Admin;

use Common\Base\ModelBase;

class NodeModel extends ModelBase
{

	/**
     * 得到节点列表
     *
     * @param int    $w
     * @param array  $search
     * @param string $fields
     * @param int    $page
     * @param int    $pageSize
     * @param string $by
     * @param string $exclude
     *
     * @return array
     */
    public function getNodeList($w=3, $search=array(), $fields='*', $page=0, $pageSize=0, $by='', $exclude='')
    {
        //查询条件
        $where =  ' where 1 ';
        if( ! empty($search))
        {
            isset($search['node_id']) && intval($search['node_id'])>0 && $where .= ' and node_id='.intval($search['node_id']);
            isset($search['node_ids']) && $search['node_ids']!='' && $where .= ' and node_id in ('.$search['node_ids'].')';
            isset($search['node_pid']) && intval($search['node_pid'])>-1 && $where .= ' and node_pid='.intval($search['node_pid']);
            isset($search['m_c_a']) && $search['m_c_a']!='' && $where .= " and m_c_a='".$search['m_c_a']."'";
            isset($search['st']) && $search['st']!='' && $where .= " and st=".intval($search['st']);
            isset($search['node_level']) && $where .= " and node_level=".intval($search['node_level']);
            isset($search['lt_node_level']) && $where .= " and node_level<".intval($search['lt_node_level']);
            isset($search['ids']) && $search['ids']!='' && $where .= " and node_id in (".$search['ids'].")";
            isset($search['node_pid_path']) && $search['node_pid_path']!='' && $where .= " and node_pid_path like '%".$search['node_pid_path']."%'";
        }
        if($exclude){ $where .= $exclude; }

        //排序
        $orderBy = ' order by ';
        $by = trim($by);
        if($by == ''){
            $orderBy .= 'node_id desc';
        }else{
            $orderBy .= $by;
        }

        //SQL
        $sql['sql_ct'] = 'select count(node_id) as tot from '.getTable('acl_node').$where;
        $sql['sql']    = 'select '.$fields.' from '.getTable('acl_node').$where.$orderBy;

        return $this->getData($w, $sql, $page, $pageSize);
    }

    /**
     * 添加节点
     *
     * @param array $dat
     *
     * @return mixed
     */
    public function addNode($dat = array())
    {
        return $this->addRow($dat, getTable('acl_node'));
    }

    /**
     * 删除节点
     *
     * @param int $node_id
     *
     * @return mixed
     */
    public function delNode($node_id = 0)
    {
        return $this->delRow(array('node_id'=>$node_id), getTable('acl_node'));
    }

     /**
     * 修改节点
     *
     * @param array $dat 需要更新的数据
     * @param int   $node_id
     *
     * @return mixed
     */
    public function updateNodeByNodeId($dat=array(), $node_id=0)
    {
        return $this->updateRow(array('node_id'=>$node_id), $dat, getTable('acl_node'));
    }

}